
cmd = {
  __call = function(self,who,cmd)
    local tokens = string.explode(cmd)
    local ok = pcall(self.handlers[tolower(tokens[1])],who,tokens)
    log("cmd: "..tostring(ok))
  end,
  handlers = {},
  addhandler = function(self,name,handler)
    name = tolower("/"..name)
    if self.handlers[name] then
      log("cmd handler '"..name.."' has been overwritten")
      self.handlers[name]=handler
      return
    else
      log("cmd handler '"..name.."' has been added")
      self.handlers[name]=handler
    end
  end, 
  removehandler = function(self,name)
    self.handlers["/"..name]= nil
  end
}
setmetatable(cmd,cmd)

function createcommand(name,t)
  local t = t or {}
  t.__name = name
  t.__call = function(self,who,tokens)
    if pcall(self[tolower(tokens[2])],who,tokens) then
      return true
    else
      self.help(who,tokens)
      return false
    end  
  end
  setmetatable(t,t)
  cmd:addhandler(name,t)
end

if false then
    createcommand("doit",{
      help = function(who,tokens)
        print("help...")
      end,
      hello = function(who,tokens)
        print("Hello "..who)
      end,
    })
    
    cmd("me","/doit lala")
    cmd("me","/doit hello")
end
